import { getToken } from '@/utils/auth'
import { toast, showConfirm, tansParams } from '@/utils/common'
import config from './config'


let timeout = 60000
//const baseUrl = config.baseUrl
//const baseUrl = 'http://localhost:8080'
const baseUrl = config.baseUrl


const upload = ( config ) => {
	// 是否需要设置 token
	const isToken = config.header.isToken;
	if (getToken() && isToken) {
	  config.header['content-type'] = "application/json; charset=utf-8"
	  config.header['Authorization'] = getToken()
	  config.header['Deviceid'] = uni.getSystemInfoSync().deviceId;
	  console.log( config.header )
	}
	// get请求映射params参数
	if (config.params) {
	  let url = config.url + '?' + tansParams(config.params)
	  url = url.slice(0, -1)
	  config.url = url
	}
	
	return new Promise((resolve, reject) => {
	uni.uploadFile({
       timeout: config.timeout || timeout,
       url: baseUrl + config.url,
       filePath: config.filePath,
       name: config.name || 'file',
       header: config.header,
       formData: config.formData,
       success: (response) => {
		   //console.log( response ) 
		   const code = response.statusCode || 200
		   if (code !== 200) {
			 console.error(  response.data.message  )
		     toast("接口处理异常,请重新上传"+ response.data.message )
		     reject( response.data.message)
			 return
		   }

		   let result = JSON.parse(response.data)
		   if ( result.code == 400 ) {
		   		toast("用户登录信息错误" )
				reject( result.msg )
				return
		   }
		   
		   
		   if (result.code !== 0) {
		   	console.error(  result.msg  )
		    toast("系统业务处理异常,请重新上传" )
		    reject( result.msg )
			return
		   }
		   console.log(  result  )
		   resolve(result)
       },
       fail: (error) => {
         // 处理上传失败的错误console.error("接口访问异常")
		  console.error("接口访问异常,请重新上传")
          console.log(error)
          toast("接口访问异常")
          reject(error)
       }
     })
	
	
	})
	
	
	/*
  // 是否需要设置 token
  const isToken = (config.headers || {}).isToken === false
  config.header = config.header || {}
  if (getToken() && !isToken) {
    config.header['Authorization'] = 'Bearer ' + getToken()
  }
  // get请求映射params参数
  if (config.params) {
    let url = config.url + '?' + tansParams(config.params)
    url = url.slice(0, -1)
    config.url = url
  }
  return new Promise((resolve, reject) => {
      uni.uploadFile({
        timeout: config.timeout || timeout,
        url: baseUrl + config.url,
        filePath: config.filePath,
        name: config.name || 'file',
        header: config.header,
        formData: config.formData,
        success: (res) => {
          let result = JSON.parse(res.data)
          const code = result.code || 200
          const msg = errorCode[code] || result.msg || errorCode['default']
          if (code === 200) {
            resolve(result)
          } else (code !== 200) {
            toast(msg)
            reject(code)
          }
        },
        fail: (error) => {
          console.error("接口访问异常")
          console.log(error)
          toast(error.errMsg)
          reject(error)
        }
      })
  })*/
}

export default upload
